package com.tsd.jxc.fin;

import com.tsd.core.utils.HlpUtils;
import com.tsd.jxc.fin.entity.JxcFinanceAccountDetail;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: FinanceConstants
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/9/1 16:38
 * @Version: 1.0
 */
public class FinanceConstants {

    public static final String MODE_JXC = "jxc";

    public static final String TYPE_SALE = "sale";
    public static final String TYPE_PCB_SALE = "pcbSale";
    public static final String TYPE_BUY = "buy";
    public static final String TYPE_PCB_BUY = "pcbBuy";
    // receivable-应收、payable-应付、cash_in-收款、cash_out-付款
    public static final String TYPE_RECEIVABLE = "receivable";
    public static final String TYPE_PAYABLE = "payable";
    public static final String TYPE_CASH_IN = "cash_in";
    public static final String TYPE_CASH_OUT = "cash_out";
    public static final String OP_ADD = "+";
    public static final String OP_SUB = "-";

    public static final String QUEUE_OP_INSERT = "insert";
    public static final String QUEUE_OP_DELETE = "delete";

    public static final String KEY_JXC_ACCOUNT_TYPE = "jxc_account_type";

    public static final String BILL_TYPE_SALE_ORDER = "sale_order";
    public static final String BILL_TYPE_SALE_ORDER_IGNORE = "sale_order_ignore";
    public static final String BILL_TYPE_SALE_RETURN = "sale_return";
    public static final String BILL_TYPE_SALE_RETURN_IGNORE = "sale_return_ignore";
    public static final String BILL_TYPE_SALE_POINT = "sale_point";
    public static final String BILL_TYPE_SALE_FEE = "sale_fee";
    public static final String BILL_TYPE_SALE_PREPARE_FEE = "sale_prepare_fee";
    public static final String BILL_TYPE_SALE_PAY = "sale_pay";

    public static final String BILL_TYPE_BUY_ORDER = "buy_order";
    public static final String BILL_TYPE_BUY_ORDER_IGNORE = "buy_order_ignore";
    public static final String BILL_TYPE_BUY_RETURN = "buy_return";
    public static final String BILL_TYPE_BUY_RETURN_IGNORE = "buy_return_ignore";
    public static final String BILL_TYPE_BUY_POINT = "buy_point";
    public static final String BILL_TYPE_BUY_FEE = "buy_fee";
    public static final String BILL_TYPE_BUY_PREPARE_FEE = "buy_prepare_fee";
    public static final String BILL_TYPE_BUY_PAY = "buy_pay";

    public static final String BIZ_NAME_FINANCE = "jxc_finance_account_detail";

    private static final Map<String, String> TYPE_MAP;
    private static final LinkedHashMap<String, String> ACCOUNT_TYPE_MAP;
    private static final Map<String, String> ACCOUNT_TYPE_OP_MAP;

    static {
        TYPE_MAP = new HashMap<>();
        TYPE_MAP.put(TYPE_SALE, "销售对账");
        TYPE_MAP.put(TYPE_BUY, "采购对账");

        ACCOUNT_TYPE_MAP = new LinkedHashMap<>();
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_SALE_ORDER, "销售订单");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_SALE_ORDER_IGNORE, "销售订单抹零");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_SALE_RETURN, "销售退货");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_SALE_RETURN_IGNORE, "销售退货抹零");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_SALE_POINT, "销售返点");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_SALE_FEE, "销售费用");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_SALE_PREPARE_FEE, "销售预付款");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_SALE_PAY, "销售收款");

        ACCOUNT_TYPE_MAP.put(BILL_TYPE_BUY_ORDER, "采购订单");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_BUY_ORDER_IGNORE, "采购订单抹零");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_BUY_RETURN, "采购退货");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_BUY_RETURN_IGNORE, "采购退货抹零");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_BUY_POINT, "采购返点");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_BUY_FEE, "采购费用");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_BUY_PREPARE_FEE, "采购预付款");
        ACCOUNT_TYPE_MAP.put(BILL_TYPE_BUY_PAY, "采购付款");

        ACCOUNT_TYPE_OP_MAP = new HashMap<>();
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_SALE_ORDER, OP_SUB);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_SALE_ORDER_IGNORE, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_SALE_RETURN, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_SALE_RETURN_IGNORE, OP_SUB);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_SALE_POINT, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_SALE_FEE, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_SALE_PREPARE_FEE, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_SALE_PAY, OP_ADD);

        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_BUY_ORDER, OP_SUB);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_BUY_ORDER_IGNORE, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_BUY_RETURN, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_BUY_RETURN_IGNORE, OP_SUB);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_BUY_POINT, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_BUY_FEE, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_BUY_PREPARE_FEE, OP_ADD);
        ACCOUNT_TYPE_OP_MAP.put(BILL_TYPE_BUY_PAY, OP_ADD);
    }

    public static Map<String, String> getAccountTypeMap() {
        return ACCOUNT_TYPE_MAP;
    }

    public static void setAccountType(String key, String value) {
        if (HlpUtils.isAllNotEmpty(key, value)) {
            ACCOUNT_TYPE_MAP.put(key, value);
        }
    }

    public static void setAccountTypeOp(String key, String value) {
        if (HlpUtils.isAllNotEmpty(key, value)) {
            ACCOUNT_TYPE_OP_MAP.put(key, value);
        }
    }

    public static void clearAccountType(String dataSource, List<String> keys) {
        if (HlpUtils.isEmptyList(keys)) {
            return;
        }
        for (String key : keys) {
            String k = String.format("%s@%s", dataSource, key);
            ACCOUNT_TYPE_MAP.remove(k);
        }
    }

    public static void clearAccountTypeOp(String dataSource, List<String> keys) {
        if (HlpUtils.isEmptyList(keys)) {
            return;
        }
        for (String key : keys) {
            String k = String.format("%s@%s", dataSource, key);
            ACCOUNT_TYPE_MAP.remove(k);
        }
    }

    public static String getTypeName(String type) {
        if (HlpUtils.isEmpty(type)) {
            return null;
        }
        return TYPE_MAP.get(type);
    }

    public static String getAccountTypeName(String dataSource, String type) {
        if (HlpUtils.isEmpty(type)) {
            return null;
        }
        String value = ACCOUNT_TYPE_MAP.get(type);
        if (HlpUtils.isEmpty(value)) {
            value = ACCOUNT_TYPE_MAP.get(String.format("%s@%s", dataSource, type));
        }
        return value;
    }

    public static String getAccountTypeOp(String dataSource, String type) {
        if (HlpUtils.isEmpty(type)) {
            return null;
        }
        String value = ACCOUNT_TYPE_OP_MAP.get(type);
        if (HlpUtils.isEmpty(value)) {
            value = ACCOUNT_TYPE_OP_MAP.get(String.format("%s@%s", dataSource, type));
        }
        return value;
    }

    public static BigDecimal getAmount(JxcFinanceAccountDetail detail) {
        if (TYPE_RECEIVABLE.equals(detail.getType())) {
            return detail.getReceivable_amount() == null ? BigDecimal.ZERO : detail.getReceivable_amount();
        }
        if (TYPE_PAYABLE.equals(detail.getType())) {
            return detail.getPayable_amount() == null ? BigDecimal.ZERO : detail.getPayable_amount();
        }
        if (TYPE_CASH_IN.equals(detail.getType())) {
            return detail.getCash_in_amount() == null ? BigDecimal.ZERO : detail.getCash_in_amount();
        }
        return detail.getCash_out_amount() == null ? BigDecimal.ZERO : detail.getCash_out_amount();
    }
}
